<?php


namespace App\Http\Controllers\Business;


use App\Http\Controllers\Controller;
use App\Models\Business\Food;
use App\Models\Business\FoodCategory;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Builder;
/**
 * @group 类目信息
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */
class FoodCategoryController extends Controller
{
    /**
     * 类目信息查看
     *
     * @queryParam  name 类目名字
     * @queryParam  food 类目下食品
     * @queryParam  food-》business 类目下食品对应商家
     *
     *
     */
    public function index(Request $request){
        $query = FoodCategory::get()->toArray();
        return response()->json($query,200);
    }
    /**
     * 类目信息查看单条
     *
     * @queryParam  name 类目名字
     * @queryParam  food 类目下食品
     * @queryParam  food-》business 类目下食品对应商家
     *
     * @bodyParam  business int  对应商家id

     * 503  没有该信息
     */
    public function show(Request $request,$pk){
        $type = $request->user_type;

        $data = FoodCategory::whereHas('foods',function (Builder $q) use ($request)
        {
            $request->has('business') && $q->where('business_id','=',$request->get('business'));
        })->where('id',$pk)->get()->toArray();
        $ids = [];
        array_walk($data,function ($value,$key) use (&$ids)
        {
           $ids [] = $value['id'];
        });
        if($data != [])
        {

            $foods = Food::whereIn('category_id',$ids)->with('business');
            if($request->has('business')){
                $foods->where('business_id','=',$request->get('business'));
            }
            if ($type ==2){
                $foods->where('business_id','=',$request->user_id);
            }
            $foods = $foods->get()->toArray();
            foreach($data as $k => $v){
                $data[$k]['foods'] = [];
                foreach ($foods as $key=>$val){
                    if($val['category_id'] == $v['id'])
                        array_push($data[$k]['foods'],$foods[$key]);
                }
            }
        }
        if($data == []){
            return response()->json(["message"=>"没有该信息"],503);
        }else{
            return response()->json($data,200);
        }
    }
    /**
     * 类目添加
     * @bodyParam  name string required 类目名字
     *
     * 503  添加失败
     *
     */
    public function store(Request $request)
    {
        $row=$request->all();
        try{
            $e = genValidate();
            $validator = Validator::make($request->all(),$e);
            if($validator->fails()){
                return response()->json($validator->errors()->messages(),400);
            }
            $data=FoodCategory::create($row);
            return response()->json(["message"=>"添加成功"],201);
        }catch (QueryException $e){
            return response()->json(["message"=>"添加失败"],503);
        }
    }
    /**
     * 类目修改
     * @bodyParam  name string required 类目名字
     *
     * 503  修改失败
     */
    public function update(Request $request, $pk){

        $row=$request->all();
        $e = genValidate();
        $validator = Validator::make($request->all(),$e);
        if($validator->fails()){
            return response()->json($validator->errors()->messages(),400);
        }
        $carkind = FoodCategory::find($pk);
        if($carkind == []){
            return response()->json(["message"=>"没有该信息"],404);
        }
        try {
            $carkind->update($row);
            return response()->json(["message"=>"修改成功"],200);
        }catch (QueryException $e){
            return response()->json(["message"=>"修改失败"],503);
        }
    }
    /**
     * 商品分类删除
     *
     *
     */
    public function destroy(Request $request,$pk){

        $data = FoodCategory::find($pk);
        if($data == []){
            return response()->json(["message"=>"没有该信息"],404);
        }
        $data->delete();
        return response()->json(["message"=>"删除成功"],200);
    }
}
